# encoding: utf-8

import os
import datetime

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

from model.music import Musician, Recording

# /recording
class RecordingHandler(webapp.RequestHandler):
    def get(self):
        pass

# /recording/new
# Add a new recording.
class RecordingNewHandler(webapp.RequestHandler):
    def get(self):
        # A form for user to input the musician information.
        path = os.path.join(os.path.dirname(__file__),
                            'template/musician_new.html')
        self.response.out.write(template.render(path, {}))

    def post(self):
        # Save the musician then redirect to the detail page.
        m = Musician(name=self.request.get("name"),
                     biography = self.request.get("biography"),
                     birthdate = datetime.date.today())
        m.put()

        self.redirect('/musician/%d' % m.key().id())

# /recording/([0-9]+)
# Show the detailed information of a recording.
class RecordingDetailHandler(webapp.RequestHandler):
    def get(self, id_str):
        #musician = Musician.get_by_id(int(id_str))
        path = os.path.join(os.path.dirname(__file__),
                            'template/recording_detail.html')
        self.response.out.write(template.render(path,
            {}))


application = webapp.WSGIApplication(
    [('/recording', RecordingHandler),
     ('/recording/new', RecordingNewHandler),
     ('/recording/([0-9]+)', RecordingDetailHandler)],
    debug = True)


def main():
    run_wsgi_app(application)


if __name__ == "__main__":
    main()
